package com.example.administrator.daydayappmarket.recommend.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.recommend.adapter.RecommendTopSpecialAdapter;
import com.example.administrator.daydayappmarket.recommend.moduls.GsonSpecial;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class RecommendTopImageSpecialSecondActivity extends AppCompatActivity implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener {

    @InjectView(R.id.activity_recommend_top_image_special_second_imageback)
    RelativeLayout imageback;
    @InjectView(R.id.activity_recommend_top_image_special_second_imagedownload)
    RelativeLayout imagedownload;
    @InjectView(R.id.activity_recommend_top_image_special_second_imagesearch)
    RelativeLayout imagesearch;
    @InjectView(R.id.activity_recommend_top_image_special_second_listView)
    PullToRefreshListView listView;

    private int index = 1;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;
    private String url;
    private List<GsonSpecial> gsonSpecials;
    private RecommendTopSpecialAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_recommend_top_image_special_second);
        ButterKnife.inject(this);

        gsonSpecials = new ArrayList<>();
        adapter = new RecommendTopSpecialAdapter(gsonSpecials,this);

        requestQueue = Volley.newRequestQueue(this);


        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        stringrequest(null);
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_listview_empty, null);
        Glide.with(this).load(R.drawable.cat_loading_transparent).into((ImageView) inflate.findViewById(R.id.layout_listView_empty_image));

        listView.setEmptyView(inflate);
        listView.setAdapter(adapter);

        listView.setOnRefreshListener(this);

        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnItemClickListener(this);
    }

    private void stringrequest(final PullToRefreshBase refreshView){
        stringRequest = new StringRequest(url + index, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    if(index == 1){
                        gsonSpecials.clear();
                    }

                    for (int i = 0; i < jsonArray.length(); i++) {
                        GsonSpecial gsonSpecial = new GsonSpecial();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String theme_type = jsonObject.getString("theme_type");
                        gsonSpecial.setTheme_type(theme_type);
                        JSONObject data = jsonObject.getJSONObject("data");
                        GsonSpecial.DataBean dataBean = new GsonSpecial.DataBean();
                        dataBean.jsonParse(data);
                        gsonSpecial.setData(dataBean);
                        gsonSpecials.add(gsonSpecial);
                    }
                    adapter.notifyDataSetChanged();
                    if (refreshView!=null){
                        refreshView.onRefreshComplete();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                if (refreshView!=null){
                    refreshView.onRefreshComplete();
                }
            }
        });

        stringRequest.setTag("recommendsecondspecial");
        requestQueue.add(stringRequest);
        requestQueue.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("recommendsecondspecial");
    }

    @OnClick({R.id.activity_recommend_top_image_special_second_imageback, R.id.activity_recommend_top_image_special_second_imagedownload, R.id.activity_recommend_top_image_special_second_imagesearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_recommend_top_image_special_second_imageback:
                finish();
                break;
            case R.id.activity_recommend_top_image_special_second_imagedownload:
                Intent intent = new Intent(this, DownloadActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_recommend_top_image_special_second_imagesearch:
                Intent intent1 = new Intent(this, SearchActivity.class);
                startActivity(intent1);
                break;
        }
    }

    //TODO 上拉下拉
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        index = 1;
        stringrequest(refreshView);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        index++;
        stringrequest(refreshView);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(this,RecommendTopImageSpecialThirdActivity.class);
        intent.putExtra("id",gsonSpecials.get(position-1).getData().getId());
        startActivity(intent);
    }
}
